package com.guxiuwei.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guxiuwei.sys.common.Result;
import com.guxiuwei.sys.entity.Leaves;
import com.guxiuwei.sys.entity.Spareparts;
import com.guxiuwei.sys.entity.User;
import com.guxiuwei.sys.mapper.SparepartsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谷秀伟
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/sys/spareparts")
public class SparepartsController {
    @Autowired
    private SparepartsMapper sparepartsMapper;

    //获取前端传入的默认显示页码，页面容量数，
    // 和搜索框查询条件，对数据库中备件信息表进行分页查询
    @GetMapping("/selectAll")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search ){
        System.out.println(pageNum+"=="+pageSize+"=="+search);
        QueryWrapper<Spareparts> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sid");
        if (StrUtil.isNotBlank(search)){
            wrapper.like("sid",search);
        }
        Page<Spareparts> sparepartsPage = sparepartsMapper.selectPage(new Page<>(pageNum, pageSize),wrapper );
        System.out.println(sparepartsPage);
        return Result.success(sparepartsPage);
    }

    //将spareparts对象存入数据库中
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('客户经理')")
    public Result<?> save(@RequestBody Spareparts spareparts){
        Spareparts res = sparepartsMapper.selectOne(Wrappers.<Spareparts>lambdaQuery()
                .eq(Spareparts::getSid, spareparts.getSid()));
        if (res != null){
            return Result.error("0","该备件已注册");
        } else {
            sparepartsMapper.insert(spareparts);
            return Result.success();
        }
    }

    //对前端传入的spareparts对象进行修改更新操作
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('客户经理')")
    public Result<?> update(@RequestBody Spareparts spareparts){
        sparepartsMapper.updateById(spareparts);
        return Result.success();
    }

    //对前端传入的spareparts对象的sid进行相关记录审批
    @PutMapping("/finish")
    @PreAuthorize("hasAuthority('云平台维护室')")
    public Result<?> finish(@RequestBody Spareparts spareparts){

        spareparts.setStatus("已完成");
        sparepartsMapper.updateById(spareparts);
        return Result.success();
    }

    //得到待审批备件出入库申请数量 返回给管理者
    @GetMapping("/countStatus")
    public Result<?> countStatus(){
        QueryWrapper<Spareparts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","待审批");
        int count = sparepartsMapper.selectCount(queryWrapper);
        return Result.success(count);
    }

}

